package com.winit.openapi.wh.calculator;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.winit.common.spi.context.CommandContext;
import com.winit.erp.spi.bpartner.vo.Bpartner;
import com.winit.oms.spi.calculator.ECommerceClearanceCalcFeeService;
import com.winit.oms.spi.calculator.command.ECommerceClearanceCommand;
import com.winit.oms.spi.calculator.vo.ECommerceClearanceResult;
import com.winit.oms.spi.calculator.vo.MerchandiseInfo;
import com.winit.openapi.constants.ApiConstant;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;
import com.winit.openapi.util.ValidateUtil;

/**
 * 电商通关计算器
 * 
 * @author hui.li2 2018年3月2日 下午5:57:08
 * @since 1.0
 */
@Controller
@RequestMapping(value = "/wh/ecommerceClearance")
public class ECommerceClearanceCalcFeeController extends BaseController {

    @Resource
    private ECommerceClearanceCalcFeeService ecommerceClearanceCalcFeeService;

    /**
     * 计费器o
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/calculate")
    public String calculate(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        ECommerceClearanceCommand command = new ECommerceClearanceCommand();
        Object obj = requestMsg.getData();
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            // TODO
            ValidateUtil.validNotNull(json, "dataSource");
            ValidateUtil.validNotNull(json, "transportMode");
            ValidateUtil.validNotNull(json, "destWarehouseCode");
            List<MerchandiseInfo> mList = new ArrayList<MerchandiseInfo>();
            if (json.containsKey("merchandiseList")) {
                JSONArray merchandiseDetailsList = json.getJSONArray("merchandiseList");
                for (Object o : merchandiseDetailsList) {
                    JSONObject merchandiseDetails = (JSONObject) o;
                    MerchandiseInfo info = new MerchandiseInfo();
                    if (merchandiseDetails.containsKey("merchandiseSerno")) {
                        info.setMerchandiseSerno(merchandiseDetails.getString("merchandiseSerno").trim());
                    }
                    if (merchandiseDetails.containsKey("merchandiseCode")) {
                        info.setMerchandiseCode(merchandiseDetails.getString("merchandiseCode").trim());
                    }
                    if (merchandiseDetails.containsKey("specification")) {
                        info.setSpecification(merchandiseDetails.getString("specification").trim());
                    }
                    if (merchandiseDetails.containsKey("quantity")) {
                        info.setQty(merchandiseDetails.getInteger("quantity"));
                    }
                    mList.add(info);
                }
            }
            command.setDataSource(json.getString("dataSource"));
            command.setTransportMode(json.getString("transportMode"));
            command.setDestWarehouseCode(json.getString("destWarehouseCode"));
            // 客户编码
            Bpartner bpartner = CommandContext.getContext().get(ApiConstant.API_BPARTNER);
            if (bpartner != null) {
                command.setCustomerCode(bpartner.getValue());
            }
            command.setMerchandiseList(mList);
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        ECommerceClearanceResult result = ecommerceClearanceCalcFeeService.calculate(command);
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("result", result);
        responseMsg.setData(data);
        return SUCCESS;
    }

}
